$(function () {
    //======================
    //======设置查询条件========
    //======================
    //内部公司
    $("#cwsfc_toolbar [name='gsId']").gsjgtree(1);
    //计划年份
    var yearSpinner = $("#cwsfc_toolbar [name='year']").numberspinner({
        min: 2000,
        max: 2100,
        editable: true
    });
    //计划月份
    var monthSpinner = $("#cwsfc_toolbar [name='month']").numberspinner({
        min: 1,
        max: 12,
        editable: true
    });

    $("#cwsfc_toolbar [name='cbjz']").click(function () {
        var year = $(yearSpinner).numberspinner('getValue');
        var month = $(monthSpinner).numberspinner('getValue');
        $.messager.confirm('提示', '是否确认对' + year + '年' + month + '月销售成本进行结转?', function (r) {
            if (r) {
                $.ajax({
                    url: $.webPath() + "/report/cwsfc/cbjz",
                    dataType: 'json',
                    type: 'POST',
                    data: {
                        year: $(yearSpinner).numberspinner('getValue'),
                        month: $(monthSpinner).numberspinner('getValue')
                    },
                    beforeSend: function () {
                        $.mask();
                    },
                    success: function (data) {
                        $.mask.hide();
                        if (data.statusCode == 200) {
                            $.messager.show({
                                title: '提示',
                                msg: data.message,
                                timeout: 3000,
                                showType: 'slide'
                            });
                            $("#cwsfc_toolbar [name='searchButton']").click();
                        } else {
                            $.messager.alert('提示', data.message, 'error');
                        }
                    },
                    error: function () {
                    }
                });
            }
        });
    });

    $("#cwsfc_toolbar [name='ymjz']").click(function () {
        var year = $(yearSpinner).numberspinner('getValue');
        var month = $(monthSpinner).numberspinner('getValue');
        $.messager.confirm('提示', '是否确认对' + year + '年' + month + '月财务收发存数据进行月末结转?', function (r) {
            if (r) {
                $.ajax({
                    url: $.webPath() + "/report/cwsfc/ymjz",
                    dataType: 'json',
                    type: 'POST',
                    data: {
                        year: $(yearSpinner).numberspinner('getValue'),
                        month: $(monthSpinner).numberspinner('getValue')
                    },
                    beforeSend: function () {
                        $.mask();
                    },
                    success: function (data) {

                        $.mask.hide();
                        if (data.statusCode == 200) {
                            $.messager.show({
                                title: '提示',
                                msg: data.message,
                                timeout: 3000,
                                showType: 'slide'
                            });
                        } else {
                            $.messager.alert('提示', data.message, 'error');
                        }
                    },
                    error: function () {
                    }
                });
            }
        });
    });

    //搜索按钮
    $("#cwsfc_toolbar [name='searchButton']").click(function () {
        $(grid).datagrid({
            queryParams: {
                year: $("#cwsfc_toolbar [name='year']").val(),
                month: $("#cwsfc_toolbar [name='month']").val(),
                gsId: $("#cwsfc_toolbar [name='gsId']").val()
            }});
    });


//======================
//========统计列表========
//======================
    var columns = [
        [
            {field: 'gsjc', title: '公司', rowspan: 2, width: 100, align: 'center'},
            {field: 'qc', title: '期初', rowspan: 2, width: 100, align: 'right', formatter: function (v, r, i) {
                var color = "";
                if (v && r.gsjc != '合计') {
                    if (v < 0) color = "red;";
                    return '<span style="color:' + color + '" >' + formatSl2(v, ",", ".") + '</span>';
                } else {
                    return formatSl2(v, ",", ".");
                }
            }},
            {title: '本期购入', colspan: 5, width: 100},
            {title: '调整', colspan: 1, width: 100},
            {title: '本期发出', colspan: 2, width: 100},
            {field: 'qm', title: '期末', rowspan: 2, width: 100, align: 'right', formatter: function (v, r, i) {
                var color = "";
                if (v && r.gsjc != '合计') {
                    if (v < 0) color = "red;";
                    return '<span style="color:' + color + '" >' + formatSl2(v, ",", ".") + '</span>';
                } else {
                    return formatSl2(v, ",", ".");
                }
            }}
        ],
        [
            {field: 'cgjxfp', title: '采购结算', width: 100, align: 'right', formatter: function (v, r, i) {
                var color = "#0000ff;";
                if (v && r.gsjc != '合计') {
                    if (v < 0) color = "red;";
                    return '<a href="javascript:void(0)" style="color:' + color + '" class="cgjs" gsid="' + r.gsid + '">' + formatSl2(v, ",", ".") + '</a>';
                } else {
                    return formatSl2(v, ",", ".");
                }
            }},
            {field: 'wljxfp', title: '物流结算', width: 100, align: 'right', formatter: function (v, r, i) {
                var color = "#0000ff;";
                if (v && r.gsjc != '合计') {
                    if (v < 0) color = "red;";
                    return '<a href="javascript:void(0)" style="color:' + color + '" class="wljs" gsid="' + r.gsid + '">' + formatSl2(v, ",", ".") + '</a>';
                } else {
                    return formatSl2(v, ",", ".");
                }
            }},
            {field: 'cgjxgs', title: '采购估收', width: 100, align: 'right', formatter: function (v, r, i) {
                var color = "#0000ff;";
                if (v && r.gsjc != '合计') {
                    if (v < 0) color = "red;";
                    return '<a href="javascript:void(0)" style="color:' + color + '" class="cggs" gsid="' + r.gsid + '">' + formatSl2(v, ",", ".") + '</a>';
                } else {
                    return formatSl2(v, ",", ".");
                }
            }},
            {field: 'wljxgs', title: '物流估收', width: 100, align: 'right', formatter: function (v, r, i) {
                var color = "#0000ff;";
                if (v && r.gsjc != '合计') {
                    if (v < 0) color = "red;";
                    return '<a href="javascript:void(0)" style="color:' + color + '" class="wlgs" gsid="' + r.gsid + '">' + formatSl2(v, ",", ".") + '</a>';
                } else {
                    return formatSl2(v, ",", ".");
                }
            }},
            {field: 'sqcx', title: '上期冲销', width: 100, align: 'right', formatter: function (v, r, i) {
                var color = "";
                if (v && r.gsjc != '合计') {
                    if (v < 0) color = "red;";
                    return '<span  style="color:' + color + '" >' + formatSl2(v, ",", ".") + '</span>';
                } else {
                    return formatSl2(v, ",", ".");
                }
            }},
            {field: 'sltz', title: '库存盘点', colspan: 1, width: 100, align: 'right', formatter: function (v, r, i) {
                var color = "#0000ff;";
                if (v && r.gsjc != '合计') {
                    if (v < 0) color = "red;";
                    return '<a href="javascript:void(0)" style="color:' + color + '" class="sltz" gsid="' + r.gsid + '">' + formatSl2(v, ",", ".") + '</a>';
                } else {
                    return formatSl2(v, ",", ".");
                }
            }},
            {field: 'xsfp', title: '销售结算', width: 100, align: 'right', formatter: function (v, r, i) {
                var color = "#0000ff;";
                if (v && r.gsjc != '合计') {
                    if (v < 0) color = "red;";
                    return '<a href="javascript:void(0)" style="color:' + color + '" class="xsjs" gsid="' + r.gsid + '">' + formatSl2(v, ",", ".") + '</a>';
                } else {
                    return formatSl2(v, ",", ".");
                }
            }},
            {field: 'nbll', title: '内部领料', width: 100, align: 'right', formatter: function (v, r, i) {
                var color = "#0000ff;";
                if (v && r.gsjc != '合计') {
                    if (v < 0) color = "red;";
                    return '<a href="javascript:void(0)" style="color:' + color + '" class="nbll" gsid="' + r.gsid + '">' + formatSl2(v, ",", ".") + '</a>';
                } else {
                    return formatSl2(v, ",", ".");
                }
            }}
        ]
    ];

    var grid = $('#cwsfc_grid').datagrid({
        url: $.webPath() + '/report/cwsfc/cwsfcList',
        toolbar: "#cwsfc_toolbar",
        rownumbers: true,
        singleSelect: true,
        collapsible: true,
        fit: true,
        pageNumber: 1,
        fitColumns: true,
        showFooter: true,
        queryParams: {
            year: $("#cwsfc_toolbar [name='year']").val(),
            month: $("#cwsfc_toolbar [name='month']").val()
        },
        onLoadSuccess: function () {
//            $(".cgjs").unbind("click").bind("click", function () {
//                showDetail('采购单结算明细', $.webPath() + "/report/cwsfc/yjscgdtj", $(this).attr("gsid"));
//            });
//            $(".cggs").unbind("click").bind("click", function () {
//                showDetail('采购估收明细', $.webPath() + "/report/cwsfc/cggs", $(this).attr("gsid"));
//            });
//            $(".wljs").unbind("click").bind("click", function () {
//                showDetail('物流结算明细', $.webPath() + "/report/cwsfc/wljs", $(this).attr("gsid"));
//            });
//            $(".wlgs").unbind("click").bind("click", function () {
//                showDetail('物流估收明细', $.webPath() + "/report/cwsfc/wlgs", $(this).attr("gsid"));
//            });
//            $(".xsjs").unbind("click").bind("click", function () {
//                showDetail('销售结算明细', $.webPath() + "/report/cwsfc/xsjs", $(this).attr("gsid"));
//            });
//            $(".nbll").unbind("click").bind("click", function () {
//                showDetail('领料明细', $.webPath() + "/report/cwsfc/lljs", $(this).attr("gsid"));
//            });
//            $(".datagrid-htable .datagrid-cell").css('text-align', 'center');

            $(".cgjs").unbind("click").bind("click", function () {
                showDetail('采购单结算明细', $.webPath() + "/report/cwsfc/detail", $(this).attr("gsid"), "cgjxfp");
            });
            $(".cggs").unbind("click").bind("click", function () {
                showDetail('采购估收明细', $.webPath() + "/report/cwsfc/detail", $(this).attr("gsid"), "cgjxgs");
            });
            $(".wljs").unbind("click").bind("click", function () {
                showDetail('物流结算明细', $.webPath() + "/report/cwsfc/detail", $(this).attr("gsid"), "wljxfp");
            });
            $(".wlgs").unbind("click").bind("click", function () {
                showDetail('物流估收明细', $.webPath() + "/report/cwsfc/detail", $(this).attr("gsid"), "wljxgs");
            });
            $(".sltz").unbind("click").bind("click", function () {
                showDetail('数量调整明细', $.webPath() + "/report/cwsfc/detail", $(this).attr("gsid"), "sltz");
            });
            $(".xsjs").unbind("click").bind("click", function () {
                showDetail('销售结算明细', $.webPath() + "/report/cwsfc/detail", $(this).attr("gsid"), "xsfp");
            });
            $(".nbll").unbind("click").bind("click", function () {
                showDetail('领料明细', $.webPath() + "/report/cwsfc/detail", $(this).attr("gsid"), "nbll");
            });
            $(".datagrid-htable .datagrid-cell").css('text-align', 'center');

        },
        columns: columns
    });
//===========================================
//==============此页面用到的函数===================
//===========================================
//    var showDetail = function (title, url, gsid) {
//        var win = $.window({
//            title: title,
//            width: 1050,
//            height: 470,
//            cache: false,
//            iconCls: 'icon-tip',
//            url: url + "?gsId=" + gsid + "&year=" + $("#cwsfc_toolbar input[name='year']").val() + "&month=" + $("#cwsfc_toolbar input[name='month']").val(),
//            modal: true,
//            buttons: []
//        });
//    }

    var showDetail = function (title, url, gsid, type) {
        var win = $.window({
            title: title,
            width: 800,
            height: 500,
            maximizable: true,
            cache: false,
            iconCls: 'icon-tip',
            url: url + "?type=" + type + "&gsId=" + gsid + "&year=" + $("#cwsfc_toolbar input[name='year']").val() + "&month=" + $("#cwsfc_toolbar input[name='month']").val(),
            modal: true,
            buttons: [],
            onMaximize: function () {
                $(this).find("#cwsfc_detail_grid").edatagrid("resize");
            },
            onRestore: function () {
                $(this).find("#cwsfc_detail_grid").edatagrid("resize");
            }
        });
    }

});
